package csel.controller.commands;

import csel.model.GameCommand;
import csel.model.Model;
import csel.model.entity.NotEnoughMoneyException;
import csel.model.entity.PlayerCharacter;
import csel.model.items.EquippableItem;
import csel.model.items.TakeableItem;
import csel.model.items.UseableItem;
import csel.view.GameViewport;

public class SellCommand implements GameCommand
{

	private Model model;
	private GameViewport gameViewport;

	public SellCommand(Model model, GameViewport gameViewport)
	{
		this.model = model;
		this.gameViewport = gameViewport;
	}

	public void execute()
	{
		TakeableItem item = gameViewport.getSellItem();
		PlayerCharacter character = model.getPlayerCharacter();
		if (item != null)
		{
			if (item instanceof EquippableItem)
			{
				try
				{
					character.sellTo(character.getInteractingCharacter(),(EquippableItem)item);
					gameViewport.accept(new HideDialogCommand(gameViewport));
					gameViewport.accept(new TradeDialogCommand(gameViewport, model));
				} catch (NotEnoughMoneyException e)
				{
					System.out.println("no money");
				}
			} else
			{
				try
				{
					character.sellTo(character.getInteractingCharacter(),(UseableItem)item);
					gameViewport.accept(new HideDialogCommand(gameViewport));
					gameViewport.accept(new TradeDialogCommand(gameViewport, model));
				} catch (NotEnoughMoneyException e)
				{
					System.out.println("no money");
				}
			}
		}
	}
}
